package com.takerfan.soundboard;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SoundBoard extends Activity {

	private static int cellW;
	private static MediaPlayer mp;
	private static TextView et;
	private static Context defContext;
	//private static String rawpath = "android.resource://com.takerfan.soundboard/raw/";
	private static String rawpath = "/sdcard/SoundBoards/";
	private static String rawfold, curpath, songLast;
	private static Button b;
	private static Map<String, MusicItem> Sounds;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Sounds = new LinkedHashMap <String, MusicItem>();
		mp = new MediaPlayer();
		mp.setOnCompletionListener(onComplete);
		rawfold = getIntent().getStringExtra("folder");
		curpath = rawpath + rawfold;
		defContext = this;
		cellW = getWindowManager().getDefaultDisplay().getWidth() / 3;
		et = (TextView) findViewById(R.id.TextView01);
		
		getInfo();
		Iterator<String> itr = Sounds.keySet().iterator();
		int i = 1;
		MusicItem mi;

		TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);	
		TableRow  tr = new TableRow (this);
		while(itr.hasNext()) {
			mi = Sounds.get(itr.next());
			tr.addView(createButton(defContext, mi));
			if (i++ % 3 == 0) {
				tr.setBaselineAligned(false);
				table.addView(tr);
				tr = new TableRow (this);
			}
		}
	}
	
	public void onStop() {
		mp.stop();
//		this.finish();
		super.onStop();
	}
	
	public void getInfo() {
		String tmp;
		try {
			BufferedReader buf = new BufferedReader(new FileReader(curpath + "/data.txt"));
			while ((tmp = buf.readLine()) != null) {
				MusicItem mi = new MusicItem(tmp);
				Sounds.put(mi.music, mi);
			}
			buf.close();
		} catch (Exception e) {
			et.setText("Error reading: " + curpath + "/data.txt");
			e.printStackTrace();
		}
	}

	private static Button createButton(final Context activity, MusicItem mi) {
		b = new Button(activity);
		b.setWidth(cellW);
		b.setText(mi.label);
		b.setTag(mi.music);
		b.setTextAppearance(defContext, R.style.style_button);
		b.setOnClickListener(handler);
		return b;
	}
	
	static View.OnClickListener handler = new View.OnClickListener() {
	    public void onClick(View v) {
	    	MusicItem mi = Sounds.get(v.getTag());
			mp.reset();
        	try {
        		mp.setDataSource(curpath + "/" + mi.music);
				mp.prepare();
				mp.start();
				et.setText("Playing: " + mi.label);
				songLast = mi.label;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				et.setText("Error opening file: " + curpath + "/" + mi.music);
				e.printStackTrace();
			}
        	
	    }
	};
	static MediaPlayer.OnCompletionListener onComplete = new MediaPlayer.OnCompletionListener() {
		public void onCompletion(MediaPlayer mp) {
			et.setText("Last played: " + songLast);
			
		}
	};
	public void stopMusic(View view) {
		mp.stop();
		et.setText("Stopped: " + songLast);
	}
}

